package com.nsn.vs.sdm.adapt.configurationParse;

import java.io.File;

import org.apache.commons.net.nntp.NewGroupsOrNewsQuery;
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

import com.nsn.vs.sdm.util.dom.UtilDOM;
 abstract public class AbstractConfParser {
	/**
	 * instance logger
	 */
	private static final Logger LOGGER= Logger.getLogger(AbstractConfParser.class);
	protected File xmlFile;
	protected Document xmlDoc;
	
	/**
	 * the sole constructor
	 * @param conf_path
	 * @throws Exception
	 */
	public AbstractConfParser(String conf_path) throws Exception
	{
		xmlFile = new File(conf_path);
		if (xmlFile.exists())
		{
			xmlDoc =UtilDOM.getXMLfromFile(xmlFile);
		}
		else
		{
			throw new RuntimeException("The given xml file does not exist," + xmlFile);
		}
	}
	
	/**
	 * Evaluates attribute value from node by the specified item name
	 * 
	 * @param node
	 * @param attributeName
	 * @return
	 */
	protected String evaluateAttributeValue (final Node node, String attributeName)
	{
		if (node.getAttributes() != null)
		{
			Node item = node.getAttributes().getNamedItem(attributeName);
			if (item != null)
			{
				return item.getTextContent();
			}
		}
		
		return null;
	}
	
	/**
	 * Evaluates attribute value from node which is matched by attribute value
	 * @param node
	 * @param attributeName
	 * @return
	 */
	protected String evaluateAttributeValueMatchByTag (final Node node, String attribute_match_name, String attribute_match_name_value,String attributeName)
	{
		if (this.isMatchByAtrributeValue(node, attribute_match_name, attribute_match_name_value))
		{
			return this.evaluateAttributeValue(node, attributeName);
		}
		return null;
	}
	
	/**
	 * filter the node which is not unique by attribute value
	 * @param node
	 * @param attributeName
	 * @param value
	 * @return boolean
	 */
	protected boolean isMatchByAtrributeValue(final Node node,String attributeName,String value)
	{
		if (value.compareTo(this.evaluateAttributeValue(node, attributeName))==0)
			return true;
		else
			return false;
	}
	
	/**
	 * evaluate node value
	 * @param node
	 * @return
	 */
	protected String evaluateNodeValue(final Node node)
	{
		if (node!=null)
			return node.getNodeValue();
		return null;
		
	}
	/**
	 * Entry to parse the specified XML file
	 * 
	 * @return 
	 */
	public abstract void parse () throws Exception;

}
